﻿using HirelSeriesH.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HirelSeriesH.Entity
{
    class ThreeColor
    {
        private static Queue queueCommand = new Queue();

        private static object lockCommand = new object();

        private static bool isRun = true;

        private static string CurrentColor = "Yellow";

        public static Queue QueueCommand { get => queueCommand; set => queueCommand = value; }
        public static object LockCommand { get => lockCommand; set => lockCommand = value; }
        public static bool IsRun { get => isRun; set => isRun = value; }

        public static void EequeueCommandQueue(string command)
        {
            lock (lockCommand)
            {
                queueCommand.Enqueue(command);
            }
        }

        public static string DequeueCommandQueue()
        {
            lock (lockCommand)
            {
                try
                {
                    return (string)queueCommand.Dequeue();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return null;
                }

            }
        }

        public static void start()
        {
            Debug.WriteLine("ThreeColor AutoRun");

            string ColorCommand;
            byte[] Command = { 0x3A, 0x01, 0x01, 0x03 };
            byte[] CommandEXE;
            while (true)
            {
                if (!IsRun)
                {
                    break;
                }
                if (Global.Global.PlcPort == null || !Global.Global.PlcPort.IsOpen)
                {
                    Thread.Sleep(50);
                    continue;
                }
                if (queueCommand.Count > 0)
                {
                    ColorCommand = DequeueCommandQueue();
                    if (ColorCommand == null || ColorCommand == "")
                    {
                        continue;
                    }
                    if ((ColorCommand == "Green" || ColorCommand == "Yellow") && CurrentColor == "Red")
                    {
                        continue;
                    }
                    if (ColorCommand == "Reset")
                    {
                        int fg = 0;
                        foreach (var channel in Global.Global.ChannelList)
                        {
                            if (channel.State == "running")
                            {
                                fg = 1;
                                break;
                            }
                        }
                        if (fg != 1)
                        {
                            //没有老化板在测试，则将指示灯设置为黄色。
                            ColorCommand = "Yellow";
                        }
                        else
                        {
                            ColorCommand = "Green";
                        }
                    }

                    if (ColorCommand == "Yellow")
                    {
                        //将指示灯设置为黄色.
                        Command[3] = 0x03;
                    }
                    else if (ColorCommand == "Green")
                    {
                        //指示灯变绿
                        Command[3] = 0x02;
                    }
                    else if (ColorCommand == "Red")
                    {
                        //指示灯变红
                        Command[3] = 0x01;
                    }
                    CommandEXE = CheckWorker.ADD(Command,false);

                    Global.Global.PlcPort.Write(CommandEXE, 0, CommandEXE.Length);
                    Thread.Sleep(500);
                    Global.Global.PlcPort.Write(CommandEXE, 0, CommandEXE.Length);
                    Thread.Sleep(500);
                    Global.Global.PlcPort.Write(CommandEXE, 0, CommandEXE.Length);

                    CurrentColor = ColorCommand;
                }
                else
                {
                    Thread.Sleep(50);
                }
            }
        }
    }
}
